//SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package board_dao

import (
	"context"
	"slices"

	"gitcode.com/eteam/api-server/dao/common"
	"gitcode.com/eteam/proto-gen-go.git/board_chat_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type MsgInfoDbItem struct {
	MsgId      string `bson:"_id"`
	BoardId    string `bson:"boardId"`
	DrawId     string `bson:"drawId"`
	Content    string `bson:"content"`
	SendUserId string `bson:"sendUserId"`
	SendTime   int64  `bson:"sendTime"`
}

func (item *MsgInfoDbItem) ToMsgInfo() *board_chat_api.MsgInfo {
	return &board_chat_api.MsgInfo{
		MsgId:      item.MsgId,
		Content:    item.Content,
		SendUserId: item.SendUserId,
		SendTime:   item.SendTime,
	}
}

type _MsgInfoDao struct {
	mc *mongo.Collection
}

func newMsgInfoDao(db *mongo.Database) (*_MsgInfoDao, error) {
	mc := db.Collection(common.DB_NS_BOARD + "chat_msg")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"boardId", 1}, {"drawId", 1}, {"sendTime", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_MsgInfoDao{mc: mc}, nil
}

func (db *_MsgInfoDao) Insert(ctx context.Context, dbItem *MsgInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_MsgInfoDao) Get(ctx context.Context, boardId, drawId, msgId string) (*MsgInfoDbItem, error) {
	retItem := &MsgInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": msgId, "boardId": boardId, "drawId": drawId}).Decode(retItem)
	return retItem, err
}

func (db *_MsgInfoDao) ListAfter(ctx context.Context, boardId, drawId string, sendTime, limit int64) ([]*MsgInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"boardId": boardId, "drawId": drawId, "sendTime": bson.M{"$gt": sendTime}},
		options.Find().SetLimit(limit).SetSort(bson.M{"sendTime": 1}))
	if err != nil {
		return nil, err
	}
	retList := []*MsgInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_MsgInfoDao) ListBefore(ctx context.Context, boardId, drawId string, sendTime, limit int64) ([]*MsgInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"boardId": boardId, "drawId": drawId, "sendTime": bson.M{"$lt": sendTime}},
		options.Find().SetLimit(limit).SetSort(bson.M{"sendTime": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*MsgInfoDbItem{}
	err = cursor.All(ctx, &retList)
	if err != nil {
		return nil, err
	}
	slices.SortFunc(retList, func(a, b *MsgInfoDbItem) int {
		return int(a.SendTime - b.SendTime)
	})
	return retList, nil
}

func (db *_MsgInfoDao) CountAfter(ctx context.Context, boardId, drawId string, sendTime int64) (uint32, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"boardId": boardId, "drawId": drawId, "sendTime": bson.M{"$gt": sendTime}})
	return uint32(count), err
}

func (db *_MsgInfoDao) GetLastMsgId(ctx context.Context, boardId, drawId string) (string, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"boardId": boardId, "drawId": drawId},
		options.Find().SetLimit(1).SetSort(bson.M{"sendTime": -1}))
	if err != nil {
		return "", err
	}
	retList := []*MsgInfoDbItem{}
	err = cursor.All(ctx, &retList)
	if err != nil {
		return "", err
	}
	if len(retList) == 1 {
		return retList[0].MsgId, nil
	}
	return "", nil
}

func (db *_MsgInfoDao) GetLastMsgTime(ctx context.Context, boardId, drawId string) (int64, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"boardId": boardId, "drawId": drawId},
		options.Find().SetLimit(1).SetSort(bson.M{"sendTime": -1}))
	if err != nil {
		return 0, err
	}
	retList := []*MsgInfoDbItem{}
	err = cursor.All(ctx, &retList)
	if err != nil {
		return 0, err
	}
	if len(retList) == 1 {
		return retList[0].SendTime, nil
	}
	return 0, nil
}
